<template>
  <div>
    <el-table 
    row-key="id" 
    :tree-props="{children: 'children'}"
    default-expand-all
    :data="menulist" 
    border 
    style="width: 100%">
      <el-table-column prop="id" label="菜单编号"> </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单"> </el-table-column>
      <el-table-column prop="icon" label="菜单图标"> </el-table-column>
      <el-table-column prop="url" label="菜单地址"> </el-table-column>
      <el-table-column label="状态"> 
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status==1">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <!-- 获得每一行的数据 {{item.row}}-->
        <template slot-scope="item">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              @click="del(item.row.id)"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
// 引入封装好的辅助型函数
import {mapGetters,mapActions} from 'vuex'
import { menuList, menuDel } from "../../util/request/api";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  computed:{
    ...mapGetters({
      menulist: "menu/getMenuList",
    })
  },
  mounted() {
    // 组件加载时获取列表
    // menuList().then((res) => {
    //   console.log(res, "菜单列表 ");
    //   // 如果通过，那么就获取数据库里的列表
    //   if (res.code == 200) {
    //     this.tableData = res.list;
    //   }
    // });
    // 
    this.getMenulist();
  },

  methods: {
    ...mapActions({
      getMenulist: "menu/getMenuAction",
    }),
    //   编辑事件
    edit(id) {

      this.$emit("edit",id);
    },
    // 删除列表
    del(id) {
      this.$confirm("此操作将删除该行信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        menuDel({ id })
          .then((res) => {
            if (res.code == 200) {
              // 重新调用列表,调用接口列表
            this.getMenulist()
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
  },
};
</script>
<style scoped>
</style>